package com.yylending.remotevideo;

import android.content.Context;
import android.media.MediaPlayer;
import android.os.Vibrator;
import com.bairuitech.anychat.AnyChatCoreSDK;
import com.bairuitech.anychat.AnyChatDefine;
import com.yyfax.common.CLog;

import java.lang.ref.WeakReference;

/**
 * Created by HenryZhang on 2017/4/13.<br>
 * Copyright © 2017 www.yylending.com. All Rights Reserved.<br><br>
 */
public class AnyChat {

	public static void initMedia(AnyChatCoreSDK anychat, Context ctx) {
		/**设置必要的音视频参数*/
		//设置本地视频采集随着设备而旋转而处理
		AnyChatCoreSDK.SetSDKOptionInt(AnyChatDefine.BRAC_SO_LOCALVIDEO_AUTOROTATION, 0);//0表示关闭，1开启
		/**摄像头硬件初始化*/
		// 启动AnyChat传感器监听
		anychat.mSensorHelper.InitSensor(ctx);
		// 初始化Camera上下文句柄
		AnyChatCoreSDK.mCameraHelper.SetContext(ctx);
		//设置SURFACE_TYPE_PUSH_BUFFERS模式
//		mSurfaceLocal.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
		// 打开本地视频预览，开始采集本地视频数据
//		mSurfaceLocal.getHolder().addCallback(AnyChatCoreSDK.mCameraHelper);
		/**打开本地音视频*/
		//打开本地视频, 第一个参数用-1表示本地，也可以用本地的真实userid
		anychat.UserCameraControl(-1, 1);
		//打开本地音频
		anychat.UserSpeakControl(-1, 1);
		/**请求远程音视频*/
		// mRemoteUserid为通话目标对象的userId;
//		int index = anychat.mVideoHelper.bindVideo(mSurfaceRemote.getHolder());
//		anychat.mVideoHelper.SetVideoUser(index, mRemoteUserId);
//		anychat.UserCameraControl(mRemoteUserId, 1);
//		anychat.UserSpeakControl(mRemoteUserId, 1);
		/**关闭远程音视频*/
		//关闭远程视频, mRemoteUserid为通话目标的userid
//		anychat.UserCameraControl(mRemoteUserId, 0);
		//关闭远程音频,
//		anychat.UserSpeakControl(mRemoteUserId, 0);
	}

	/**
	 * 根据配置文件配置视频参数
	 */
	public static void applyVideoConfig(Context ctx) {
//		ConfigEntity configEntity = ConfigService.LoadConfig(ctx);
//		if (configEntity.mConfigMode == 1) {// 自定义视频参数配置
//			// 设置本地视频编码的码率（如果码率为0，则表示使用质量优先模式）
//			AnyChatCoreSDK.SetSDKOptionInt(AnyChatDefine.BRAC_SO_LOCALVIDEO_BITRATECTRL, configEntity.mVideoBitrate);
////			if (configEntity.mVideoBitrate == 0) {
//			// 设置本地视频编码的质量
//			AnyChatCoreSDK.SetSDKOptionInt(AnyChatDefine.BRAC_SO_LOCALVIDEO_QUALITYCTRL, configEntity.mVideoQuality);
////			}
//			// 设置本地视频编码的帧率
//			AnyChatCoreSDK.SetSDKOptionInt(AnyChatDefine.BRAC_SO_LOCALVIDEO_FPSCTRL, configEntity.mVideoFps);
//			// 设置本地视频编码的关键帧间隔
//			AnyChatCoreSDK.SetSDKOptionInt(AnyChatDefine.BRAC_SO_LOCALVIDEO_GOPCTRL, configEntity.mVideoFps * 4);
//			// 设置本地视频采集分辨率
//			AnyChatCoreSDK.SetSDKOptionInt(AnyChatDefine.BRAC_SO_LOCALVIDEO_WIDTHCTRL, configEntity.mResolutionWidth);
//			AnyChatCoreSDK.SetSDKOptionInt(AnyChatDefine.BRAC_SO_LOCALVIDEO_HEIGHTCTRL, configEntity.mResolutionHeight);
//			// 设置视频编码预设参数（值越大，编码质量越高，占用CPU资源也会越高）
//			AnyChatCoreSDK.SetSDKOptionInt(AnyChatDefine.BRAC_SO_LOCALVIDEO_PRESETCTRL, configEntity.mVideoPreset);
//		}
//		// 让视频参数生效
//		AnyChatCoreSDK.SetSDKOptionInt(AnyChatDefine.BRAC_SO_LOCALVIDEO_APPLYPARAM, configEntity.mConfigMode);
//		// P2P设置
//		AnyChatCoreSDK.SetSDKOptionInt(AnyChatDefine.BRAC_SO_NETWORK_P2PPOLITIC, configEntity.mEnableP2P);
//		// 本地视频Overlay模式设置
//		AnyChatCoreSDK.SetSDKOptionInt(AnyChatDefine.BRAC_SO_LOCALVIDEO_OVERLAY, configEntity.mVideoOverlay);
//		// 回音消除设置
//		AnyChatCoreSDK.SetSDKOptionInt(AnyChatDefine.BRAC_SO_AUDIO_ECHOCTRL, configEntity.mEnableAEC);
//		// 平台硬件编码设置
//		AnyChatCoreSDK.SetSDKOptionInt(AnyChatDefine.BRAC_SO_CORESDK_USEHWCODEC, configEntity.mUseHWCodec);
//		// 视频旋转模式设置
//		AnyChatCoreSDK.SetSDKOptionInt(AnyChatDefine.BRAC_SO_LOCALVIDEO_ROTATECTRL, configEntity.mVideoRotateMode);
//		// 本地视频采集偏色修正设置
//		AnyChatCoreSDK.SetSDKOptionInt(AnyChatDefine.BRAC_SO_LOCALVIDEO_FIXCOLORDEVIA, configEntity.mFixColorDeviation);
//		// 视频GPU渲染设置
//		AnyChatCoreSDK.SetSDKOptionInt(AnyChatDefine.BRAC_SO_VIDEOSHOW_GPUDIRECTRENDER, configEntity.mVideoShowGPURender);
//		// 本地视频自动旋转设置
//		AnyChatCoreSDK.SetSDKOptionInt(AnyChatDefine.BRAC_SO_LOCALVIDEO_AUTOROTATION, configEntity.mVideoAutoRotation);
	}

	/**
	 * BaseEvent.OnAnyChatConnectMessage
	 */
	public static final int EVENT_CONNECT_SERVER_FAILED = 1;
	/**
	 * BaseEvent.OnAnyChatLinkCloseMessage
	 */
	public static final int EVENT_CONNECT_BREAK_FAILED = 2;
	/**
	 * BaseEvent.OnAnyChatEnterRoomMessage
	 */
	public static final int EVENT_ENTER_ROOM_FAILED = 3;
	/**
	 * ObjectEvent.OnAnyChatObjectEvent
	 */
	public static final int EVENT_CONNECT_BUSINESS_HALL_FAILED = 4;

	private static WeakReference<MediaPlayer> mMediaPlayer;

	/***
	 * 播放接收到呼叫音乐提示音、震动
	 * @param context    上下文
	 */
	public static void playCallingSession(Context context) {
		try {
			MediaPlayer mediaPlayer = MediaPlayer.create(context, R.raw.call);
			mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
				@Override
				public void onCompletion(MediaPlayer mp) {
					if (mMediaPlayer != null && mMediaPlayer.get() != null)
						mMediaPlayer.get().start();
				}
			});
			mediaPlayer.start();
			mMediaPlayer = new WeakReference<>(mediaPlayer);

			Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
			vibrator.vibrate(new long[]{500, 500}, 0);
		} catch (Exception e) {
			CLog.i("AnyChat", "media-stop : er");
		}
	}

	public static void stopCallingSession(Context context) {
		try {
			if (mMediaPlayer != null && mMediaPlayer.get() != null) {
				MediaPlayer mediaPlayer = mMediaPlayer.get();
				mediaPlayer.pause();
				mediaPlayer.stop();
				mediaPlayer.release();
			}

			Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
			vibrator.cancel();
		} catch (Exception e) {
			CLog.i("AnyChat", "media-stop : er");
		}
	}
}
